package com.fish.anne.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.fish.anne.service.SystemRoleActionManager;
import com.fish.anne.model.SystemRoleAction;
import com.fish.anne.webapp.action.BaseAction;

import java.util.List;

public class SystemRoleActionAction extends BaseAction implements Preparable {
    private SystemRoleActionManager systemRoleActionManager;
    private List systemRoleActions;
    private SystemRoleAction systemRoleAction;
    private Long  raId;

    public void setSystemRoleActionManager(SystemRoleActionManager systemRoleActionManager) {
        this.systemRoleActionManager = systemRoleActionManager;
    }

    public List getSystemRoleActions() {
        return systemRoleActions;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String systemRoleActionId = getRequest().getParameter("systemRoleAction.raId");
            if (systemRoleActionId != null && !systemRoleActionId.equals("")) {
                systemRoleAction = systemRoleActionManager.get(new Long(systemRoleActionId));
            }
        }
    }

    public String list() {
        systemRoleActions = systemRoleActionManager.getAll();
        return SUCCESS;
    }

    public void setRaId(Long  raId) {
        this. raId =  raId;
    }

    public SystemRoleAction getSystemRoleAction() {
        return systemRoleAction;
    }

    public void setSystemRoleAction(SystemRoleAction systemRoleAction) {
        this.systemRoleAction = systemRoleAction;
    }

    public String delete() {
        systemRoleActionManager.remove(systemRoleAction.getRaId());
        saveMessage(getText("systemRoleAction.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (raId != null) {
            systemRoleAction = systemRoleActionManager.get(raId);
        } else {
            systemRoleAction = new SystemRoleAction();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (systemRoleAction.getRaId() == null);

        systemRoleActionManager.save(systemRoleAction);

        String key = (isNew) ? "systemRoleAction.added" : "systemRoleAction.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}